package com.hospital.workstation.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Console;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

public class DateUtil extends cn.hutool.core.date.DateUtil {

    public static Date asDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date asDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate asLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime asLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static LocalDateTime asLocalDateTime(String date) {
        DateTime parse = parse(date);
        return Instant.ofEpochMilli(parse.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static List<Long> getAge(Date date) {
        long betweenYear = DateUtil.betweenMonth(date, new Date(), true);
        System.out.println(betweenYear);
        long l = betweenYear / 12;
        long m = betweenYear % 12;

        List<Long> objects = new ArrayList<>();
        objects.add(l);
        objects.add(m);
        return objects;
    }

    public static Date getDateByDayAndTime(String day, String time) {
        if ("".equals(day) || "".equals(time)) {
            return new Date();
        }
        return parse(day + " " + time);
    }

    /**
     * 根据出生日期获取年龄和月份
     *
     * @param birthday 出生日期
     * @return
     */
    public static Map<String, Integer> getAgeAndMonthByBirthday(Date birthday) {
        Map<String, Integer> map = new HashMap<>();
        // 出生日期日历对象
        Calendar b = Calendar.getInstance();
        b.setTime(birthday);
        // 当前日期日历对象
        Calendar now = Calendar.getInstance();
        // 计算
        int year = now.get(Calendar.YEAR) - b.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) - b.get(Calendar.MONTH);
        if (month < 0) {
            month = 12 - b.get(Calendar.MONTH) + now.get(Calendar.MONTH);
            year -= 1;
        }
        map.put("year", year);
        map.put("month", month);
        return map;
    }


    public static void main(String[] args) throws ParseException {
        /*Date dateByDayAndTime = getDateByDayAndTime("2023-03-09", "11:10:56");
        System.out.println(dateByDayAndTime);*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Integer> map = getAgeAndMonthByBirthday(format.parse("1987-01-26"));
        map.forEach((k, v) -> Console.log(k + "-" + v));
    }


}
